package com.springbootweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author 你咋那么帅
 * @Date 2020/1/16
 */
@Controller
public class LoginController {

    @PostMapping("/login")//这个相当于只接收post请求的映射
    public String login(@RequestParam("username")String username,
                        @RequestParam("password")String password,
                        Map<String,Object> map, HttpSession session) {
        session.setAttribute("loginUser",username );
        if (!StringUtils.isEmpty(username)&&"123456".equals(password)){
           //由自定义的MyMvcConfig类中的addViewControllers方法接收
            return "redirect:/dashboard.html";
        }
        else {
            //springmvc会默认把这个放入request域中（不知道为啥...）
            map.put("msg", "密码错误请重新输入");
            //由Thymeleaf模板引擎解析跳转
            return "login";
        }
    }

}
